package com.ccp.dev.workflow.service;

import com.alibaba.fastjson.JSONObject;
import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.workflow.dao.AgentConditionDao;
import com.ccp.dev.workflow.model.AgentCondition;
import com.ccp.dev.workflow.model.BpmUserCondition;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * ClassDescribe 条件代理的配置 Service类
 * @author: fanruiqi
 * Date: 2019-09-20
 * Since:1
 * To change this template use File | Settings | File Templates.
 */
@Service
public class AgentConditionService extends BaseService<AgentCondition> {

    @Resource
    private AgentConditionDao agentConditionDao;
    @Resource
    private BpmNodeUserService bpmNodeUserService;

    /**
     * 计算规则是否成立
     * @param condition 规则实体
     * @param tableId 表id
     * @param formVars 流程变量
     * @return
     */
    public boolean conditionCheck(AgentCondition condition, String tableId, Map<String, Object> formVars) {
        Boolean isPassCondition = true;
        JSONObject jsonObject = JSONObject.parseObject(condition.getCondition());
        if (BeanUtils.isEmpty(jsonObject)) {
            return isPassCondition;
        }

        // 如果表id为空 或者 与现用表单不匹配，则认为规则成立
        String currentTableId = jsonObject.getString("tableId");
        if (StringUtil.isEmpty(currentTableId) || !currentTableId.equals(tableId)) {
            return isPassCondition;
        }

        BpmUserCondition bpmUserCondition = new BpmUserCondition();
        bpmUserCondition.setFormIdentity(currentTableId);
        bpmUserCondition.setCondition(jsonObject.getString("condition"));
        isPassCondition = bpmNodeUserService.conditionCheck(bpmUserCondition, tableId, formVars);
        return isPassCondition;
    }
}
